package com.example.tanlee.ocrgraduation.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import com.example.tanlee.ocrgraduation.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.io.File;

/**
 * 常用的图片加载类,主要是对Universal-Image-Loader 进行再一次封装
 * Created by tanlee on 2015/10/15.
 */
public class ImageLoaderUtil {

    static File extDir = new File(Environment.getExternalStorageDirectory()
            .toString() + "/eagle/imageloader");

    public static ImageLoaderConfiguration getimageconfig(Context context) {
        // 若不存在，则自动创建
        if (!extDir.exists()) {
            extDir.mkdirs();
        }
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
//		    .memoryCacheExtraOptions(10, 10)
//          .threadPoolSize(3)//线程池内加载的数量
//          .threadPriority(Thread.NORM_PRIORITY -2)
//          .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new WeakMemoryCache()) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
//         .tasksProcessingOrder(QueueProcessingType.LIFO)
                .diskCache(new UnlimitedDiscCache(extDir))//自定义缓存路径
//         .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
//         .imageDownloader(new BaseImageDownloader(context,5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
//         .imageDecoder(new BaseImageDecoder(false)) // default
//         .writeDebugLogs() // Remove for releaseapp
            .build();//开始构建
        return config;
    }

    public static DisplayImageOptions getNoCashImageOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.img_preloaded) // resource or
                .showImageOnLoading(R.mipmap.img_preloaded) // drawable
                .showImageOnFail(R.mipmap.orange_location) // resource or//
                .resetViewBeforeLoading(false) // default
                .cacheInMemory(false) // default
                .cacheOnDisk(true) // default
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .build();
        return options;
    }

    public static DisplayImageOptions getNoCashNoDiskImageOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.img_preloaded) // resource or
                .showImageOnLoading(R.mipmap.img_preloaded) // drawable
                .showImageOnFail(R.mipmap.orange_location) // resource or//
                .resetViewBeforeLoading(false) // default
                .cacheInMemory(false) // default
                .cacheOnDisk(false) // default
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new SimpleBitmapDisplayer()) // default
                .build();
        return options;
    }

    public static DisplayImageOptions getCashImageOptions(int round) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.img_preloaded) // resource or
                .showImageOnFail(R.mipmap.img_load_fail) // resource or
                .showImageOnLoading(R.mipmap.img_preloaded) // drawable
                .resetViewBeforeLoading(false) // default
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new RoundedBitmapDisplayer(round)) // default
                .build();
        return options;
    }

    /**
     * 设置圆角显示
     *
     * @param round
     * @return
     */
    public static DisplayImageOptions getRoundImageOptions(int round) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.orange_location) // resource
                .showImageOnLoading(R.mipmap.orange_location) // or
                .showImageOnFail(R.mipmap.orange_location) // resource or
                .resetViewBeforeLoading(true) // default
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.EXACTLY) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new RoundedBitmapDisplayer(round)) // default
                .build();
        return options;
    }

    public static DisplayImageOptions getRoundImageOptionsWithOutCache(int round) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.orange_location)
                .showImageOnLoading(R.mipmap.orange_location)
                .showImageOnFail(R.mipmap.orange_location) // resource or
                .resetViewBeforeLoading(true) // default
                .cacheInMemory(false) // default
                .cacheOnDisk(false) // default
                .considerExifParams(false) // default
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
                .displayer(new RoundedBitmapDisplayer(round)) // default
                .build();
        return options;
    }
}
